package soft.edu.common;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import java.io.Serializable;

//这个注解的作用：保证序列化json的时候，如果是null的对象，key也会消失
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//用泛型声明这个类
public class ServerResponse<T> implements Serializable {

    private int status;
    private String msg;
    //表示将要使用的泛型的数据类型为data
    private T data;

    //方法1
    private ServerResponse(int status){
        this.status = status;
    }
    //方法2
    private ServerResponse(int status,T data){
        this.status=status;
        this.data=data;
    }
    //方法3
    private ServerResponse(int status,String msg,T data){
        this.status=status;
        this.msg=msg;
        this.data=data;
    }
    //方法4
    private ServerResponse(int status,String msg){
        this.status=status;
        this.msg=msg;
    }

    //这个方法据说特别重要
//    这个注解的作用是：使这个方法不在序列化的结果当中
    @JsonIgnore
    public boolean isSuccess(){
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    //三个变量的getter方法
    public int getStatus(){
        return status;
    }
    public T getData() {
        return data;
    }
    public String getMsg() {
        return msg;
    }

    public static <T> ServerResponse createBySuccess(){
        //对应方法1
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String msg){
        //对应方法4
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public static <T> ServerResponse createBySuccess(T data){
        //对应方法2
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    public static <T> ServerResponse createBySuccess(String msg,T data){
        //对应方法3
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }

    public static <T> ServerResponse<T> createByError(String 分数信息查询不到){
        //对应方法4
        return new ServerResponse<T>
                (ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
    }

    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage){
        //对应方法4
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage);
    }

    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){
        //对应方法4
        return new ServerResponse<T>(errorCode,errorMessage);
    }

    //    需要登录 NEED_LOGIN
    public static <T> ServerResponse<T> createByNeedLoginMessage(String msg){
        //对应方法4
        return new ServerResponse<T>(ResponseCode.NEED_LOGIN.getCode(),msg);
    }
}
